//
//  HWBookIndexView.m
//  嗨文
//
//  Created by 响当当 on 5/21/19.
//  Copyright © 2019 dongzheng. All rights reserved.
//

#import "HWBookIndexView.h"
#import "HWTBookIndexCell.h"

@interface HWBookIndexView()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong)UITableView *tableView;
@property (nonatomic, strong)NSMutableArray *infoArr;
@property (nonatomic, strong)NSString *pageNum;
@property (nonatomic, assign)BOOL hasNextPage;
@end

@implementation HWBookIndexView

- (NSMutableArray *)infoArr
{
    if (!_infoArr)
    {
        _infoArr = [[NSMutableArray alloc]init];
    }
    return _infoArr;
}
- (UITableView *)tableView
{
    if (!_tableView)
    {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0,SCREEN_WIDTH,SCREEN_HEIGHT) style:UITableViewStyleGrouped];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = 0;
        
        //上拉刷新
        MJRefreshAutoNormalFooter *footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadNextChapter)];
        //设置上拉刷新属性
        [footer setTitle:@"Loading ..." forState:MJRefreshStateRefreshing];
        [footer setTitle:@"" forState:MJRefreshStateIdle];
        [footer setTitle:@"没有更多了" forState:MJRefreshStateNoMoreData];
        
        // 设置字体
        footer.stateLabel.font = [UIFont systemFontOfSize:17];
        
        // 设置颜色
        footer.stateLabel.textColor = LCHexColor(0x1f1f1f);
        
        _tableView.mj_footer = footer;
    }
    return _tableView;
}
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        self.backgroundColor  = [UIColor clearColor];
        self.pageNum = @"1";
        self.hasNextPage = YES;
        
        [self addSubview:self.tableView];
    }
    return self;
}

#pragma mark - tableView delegate & datasource
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.infoArr.count;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    return 65*kUIScaleBasePlus;
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 40*kUIScaleBasePlus;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *footView = [[UIView alloc]initWithFrame:CGRectMake(0,0,self.frame.size.width,40*kUIScaleBasePlus)];
    footView.backgroundColor = LCHexColor(0xf2f2f2);
    
    UILabel *label = [HWUtilties creatLabelWithFrame:CGRectMake(15*kUIScaleBasePlus,5*kUIScaleBasePlus,self.frame.size.width,30*kUIScaleBasePlus)
                                        textAligment:NSTextAlignmentLeft
                                            textClor:LCHexColor(0x545454)
                                            textFont:16*kUIScaleBasePlus];
    label.text = @"正文卷";
    [footView addSubview:label];
    
    return footView;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    HWTBookIndexCell *cell = [tableView dequeueReusableCellWithIdentifier:@"indexCell"];
    if (!cell)
    {
        cell = [[HWTBookIndexCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"indexCell"];
        cell.selectionStyle = 0;
    }
    
    if ([self.infoArr isValidArray])
    {
        HWIndexModel *model = [[HWIndexModel alloc]initWithDic:self.infoArr[indexPath.row]];
        [cell getIndexInfo:model];
    }
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    HWIndexModel *model = [[HWIndexModel alloc]initWithDic:self.infoArr[indexPath.row]];
    if (self.loadChapterBlock)
    {
        self.loadChapterBlock([model.chapterId stringValue],[model.chapterCoin stringValue]);
    }
}

- (void)getDate:(NSArray *)arr hasNetPage:(NSNumber *)hasNetPage
{
    [SVProgressHUD dismiss];
    if ([arr isValidArray])
    {
        if (self.hasNextPage)
        {
            [self.infoArr addObjectsFromArray:arr];
            self.hasNextPage = [hasNetPage boolValue];
            [self.tableView reloadData];
        }
        else
        {
            self.tableView.mj_footer.hidden = YES;
        }
    }
}

- (void)loadNextChapter
{
    WeakSelf(self)
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        
        if (weakSelf.upLoadIndexBlock)
        {
            if (self.hasNextPage)
            {
                NSInteger num = self.pageNum.integerValue + 1;
                self.upLoadIndexBlock([NSString stringWithFormat:@"%ld",num],@"30");
                self.pageNum = [NSString stringWithFormat:@"%ld",num];
            }
            else
            {
                self.upLoadIndexBlock(@"1",@"30");
            }
        }
        [weakSelf.tableView.mj_footer endRefreshing];
        
    });
}

@end
